package io.gbznt.modules.buss.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussInspectionPlanEntity;
import io.gbznt.modules.buss.entity.BussPublicNoticeEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussInspectionPlanService;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysUserService;

/**
 * 巡检计划
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:34
 */
@RestController
@RequestMapping("buss/bussinspectionplan")
public class BussInspectionPlanController {
	@Autowired
	private BussInspectionPlanService bussInspectionPlanService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("buss:bussinspectionplan:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = bussInspectionPlanService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("buss:bussinspectionplan:info")
	public R info(@PathVariable("id") String id) {
		String tkNames = "";
		BussInspectionPlanEntity bussInspectionPlan = bussInspectionPlanService.getById(id);
		if (StringUtils.isNotBlank(bussInspectionPlan.getHandler())) {// 巡检人
			SysUserEntity user = sysUserService.getById(bussInspectionPlan.getHandler());
			if (user != null && StringUtils.isNotBlank(user.getName())) {
				bussInspectionPlan.setHandlerName(user.getName());
			}
		}
		if (StringUtils.isNotBlank(bussInspectionPlan.getBussFarmlandInfoIds())) {
			QueryWrapper<BussFarmlandInfoEntity> ew = new QueryWrapper<BussFarmlandInfoEntity>();
			ew.in("id", bussInspectionPlan.getBussFarmlandInfoIds().split(","));
			List<BussFarmlandInfoEntity> list = bussFarmlandInfoService.list(ew);
			for (BussFarmlandInfoEntity tk : list) {// 田块
				tkNames += tk.getName() + ",";
			}
		}
		// tkNames=tkNames.substring(0,tkNames.length()-1);
		bussInspectionPlan.setTkNames(tkNames);

		return R.ok().put("bussInspectionPlan", bussInspectionPlan);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("buss:bussinspectionplan:save")
	public R save(@RequestBody BussInspectionPlanEntity bussInspectionPlan) {
		ValidatorUtils.validateEntity(bussInspectionPlan, AddGroup.class);

		bussInspectionPlan.setCreateId(ShiroUtils.getUserId() + "");
		bussInspectionPlan.setCreateTime(new Date());
		bussInspectionPlan.setCreateName(ShiroUtils.getUserName());
		bussInspectionPlanService.save(bussInspectionPlan);

		return R.ok().put("bid", bussInspectionPlan.getId());
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("buss:bussinspectionplan:update")
	public R update(@RequestBody BussInspectionPlanEntity bussInspectionPlan) {
		ValidatorUtils.validateEntity(bussInspectionPlan, AddGroup.class);

		bussInspectionPlan.setUpdateId(ShiroUtils.getUserId() + "");
		bussInspectionPlan.setUpdateTime(new Date());
		bussInspectionPlan.setUpdateName(ShiroUtils.getUserName());
		Boolean re = bussInspectionPlanService.updateById(bussInspectionPlan);// 全部更新

		return re ? R.ok() : R.oldVersion();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("buss:bussinspectionplan:delete")
	public R delete(@RequestBody String[] ids) {
		bussInspectionPlanService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

	// 每隔2秒执行一次
	@Scheduled(fixedRate = 2000)
	public void testTasks() {
		System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
	}

	/**
	 * 改变巡检计划状态
	 */
	@RequestMapping("/changeStatus/{id}/{status}")
	public R changeStatus(@PathVariable("id") String id, @PathVariable("status") String status) {
		BussInspectionPlanEntity bussInspectionPlan = bussInspectionPlanService.getById(id);
		bussInspectionPlan.setStatus(Integer.parseInt(status));
		Boolean re = bussInspectionPlanService.updateById(bussInspectionPlan);// 全部更新
		return re ? R.ok() : R.oldVersion();
	}

}
